/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.demo.demos.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@RestController
public class BasicController {

    @RequestMapping("/")
    String index(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        Integer count = (Integer) application.getAttribute("count");
        if (null == count){
            count = 0;
        }
        ++count;
        application.setAttribute("count", count);

        StringBuffer sb = new StringBuffer();
        sb.append("<h1>Jar 包部署</h1>");
        sb.append("<h1>你好，访问者，本站已被访问 ");
        sb.append(count);
        sb.append("   次!</h1>");
        sb.append("<h1>你访问的IP地址是:" + request.getRemoteAddr());
        sb.append("</h1>");
        sb.append("<h1>服务器端口是");
        sb.append(request.getLocalPort());
        sb.append("</h1>");
        return sb.toString();
    }

}
